using System;
using System.Collections.Generic;
using System.Text;
using IAS.MacroRenderings.Data;
using FileHelpers;
using System.Configuration;
namespace IAS.MacroRenderings.Helper
{
    /// <summary> 
    /// </summary>
    public static class Utility
    {
        /// <summary>
        /// quick way of reading query string and query from a file that is
        /// | separated
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static DbConn GetDBDetails(string FilePath)
        {

            string file = FilePath;
            DbConn db = new DbConn();
            FileHelperEngine engine = new FileHelperEngine(typeof(CSVRecord));
            CSVRecord[] res = engine.ReadFile(file) as CSVRecord[];
            
            foreach (CSVRecord c in res)
            {
                db.ConnString = GetConnStringFromWebConfig(c.Key);
                db.Query = c.Value;
            }

            return db;
        }

        /// <summary>
        /// for given key get it from web.config
        /// </summary>
        /// <param name="ConfigKey"></param>
        /// <returns></returns>
        public static string GetConnStringFromWebConfig(string ConfigKey) {
            return ConfigurationManager.ConnectionStrings[ConfigKey].ConnectionString;
        }
    }
    
    /// <summary>
    /// quick holder class
    /// </summary>
    public sealed  class DbConn {
        string connString = "";
        public string ConnString {
            set {
                connString = value;
            }
            get {
                return connString;
            }
        }
        string query = "";
        public string Query {

            set
            {
                query = value;
            }
            get
            {
                return query;
            }
        }
    }
}
